#!/usr/bin/env echo Warning: this library should be sourced rather than executed

# ostype(): Define OSTYPE variable of current system
ostype() {

    osname=$(uname -s)
    # assume no os type
    OSTYPE="UNKNOWN"
    
    case $osname in
	"FreeBSD") OSTYPE="FREEBSD"
	    ;;
	"SunOS") OSTYPE="SOLARIS"
	    ;;
	"Linux") OSTYPE="LINUX"
	    ;;
    esac
    return 0
}

evenodd() {
    
    # determine the even/odd status of number
    LAST_DIGIT=$(echo $1 | sed 's/\(.*\)\(.\)$/\2/')
    case $LAST_DIGIT in
        0|2|4|6|8)
            return 1
            ;;
        *)
            return 0
            ;;
    esac
}

isalive() {

    NODE=$1
    ping -c 3 $NODE >/dev/null 2>&1
    if [ $? -eq 0 ]; then
        return 1
    else
        return 0
    fi
}

is_in_path() {
    # Given a command and the PATH, try to find the command.
    # Returns 0 if found and executable, 1 if not.
    # Note that this temporarily modifies the IFS (input field separator)
    
    cmd=${1}
    path=${2}
    retval=1
    oldIFS=${IFS}
    IFS=":"
        
    for folder in ${path}
    do
        if [ -x ${folder}/${cmd} ]; then
            retval=0
        fi
    done
    
    IFS=${oldIFS}
    return ${retval}
}

check_for_cmd_in_path() {

    var=${1}

    # ${var#expr} returns everything after the match for 'expr' in the variable
    # value (if any), and ${var%expr} returns everything that doesn't match
    # (in this case, just the first character).
    
    if [ "${var}" != "" ] ; then
    
        if [ "${var%${var#?}}" = "/" ] ; then
        
            if [ ! -x ${var} ] ; then
            
              return 1
            fi            

        elif ! is_in_path ${var} $PATH ; then
        
            return 2
        fi
                    
    fi    
}

valid_alphnum() {

    # Validate arg: returns 0 if all upper+lower+digits, 1 otherwise
    # Remove all unacceptable chars
    compressed="$(echo $1 | sed -e 's/[^[:alnum:]]//g')"
  
    if [ "$compressed" != "$1" ] ; then
        return 1
    else
        return 0
    fi
}

month_num_to_name() {
    month="NONE"

    # Sets the variable 'month' to the appropriate value
    case $1 in
        1 ) month="Jan"    ;;  2 ) month="Feb"    ;;
        3 ) month="Mar"    ;;  4 ) month="Apr"    ;;
        5 ) month="May"    ;;  6 ) month="Jun"    ;;
        7 ) month="Jul"    ;;  8 ) month="Aug"    ;;
        9 ) month="Sep"    ;;  10) month="Oct"    ;;
        11) month="Nov"    ;;  12) month="Dec"    ;;
        * ) echo "$0: Unknown numeric month value $1" >&2; exit 1
    esac
    printf $month
    return 0
}

format_number() {

    # Note that we assume that  '.' is the decimal separator in
    # the INPUT value to this script. The decimal separator in the output value is
    # '.' unless specified by the user with the -d flag
  
    integer=$(echo $1 | cut -d. -f1)              # left of the decimal
    decimal=$(echo $1 | cut -d. -f2)              # right of the decimal
  
    if [ $decimal != $1 ]; then
      # There's a fractional part, so let's include it.
      result="${DD:="."}$decimal"
    fi
  
    thousands=$integer
  
    while [ $thousands -gt 999 ]; do
      remainder=$(($thousands % 1000))    # three least significant digits
  
      while [ ${#remainder} -lt 3 ] ; do  # force leading zeros as needed
        remainder="0$remainder"
      done
  
      thousands=$(($thousands / 1000))    # to left of remainder, if any
      result="${TD:=","}${remainder}${result}"    # builds right to left
    done
  
    nicenum="${thousands}${result}"
    if [ ! -z $2 ] ; then
      echo $nicenum
    fi    
}